package com.sms.platform.controller;

import com.sms.platform.common.model.Standard_Submit;
import com.sms.platform.exception.SmsInterfaceException;
import com.sms.platform.service.QueueService;
import com.sms.platform.service.SmsCheckService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author xjh
 * @date 2020-08-11
 */
@Controller
@Slf4j
public class SmsController {

    @Autowired
    private SmsCheckService smsCheckService;

    @Autowired
    private QueueService queueService;

    @RequestMapping("/smsInterface")
    public void sms(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String clientAddress = this.getIpAddress(request);
        String pwd = request.getParameter("pwd");
        String clientID = request.getParameter("clientID");
        String mobile = request.getParameter("mobile");
        String content = request.getParameter("content");
        String srcID = request.getParameter("srcID");
        try {
            log.info("短信发送方信息---> clientID:{},mobile:{},content:{},srcID:{},ip:{},pwd:{}", clientID, mobile, content,
                    srcID, clientAddress, pwd);
            //校验用户发送数据的合法性
            List<Standard_Submit> listSubmit = smsCheckService.checkSms(clientID, clientAddress, content, pwd, mobile, srcID);
            queueService.sendSmsToMQ(listSubmit);
            //数据发送到MQ
            response.getWriter().println("0");
        } catch (SmsInterfaceException e) {
            log.error(e.getMsg(), e);
            response.getWriter().println(e.getCode());
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

    private String getIpAddress(HttpServletRequest request) {
        String ip = null;
        try {
            // 获取用户真是的地址
            String Xip = request.getHeader("X-Real-IP");
            // 获取多次代理后的IP字符串值
            String XFor = request.getHeader("X-Forwarded-For");
            if (StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) {
                // 多次反向代理后会有多个IP值，第一个用户真实的IP地址
                int index = XFor.indexOf(",");
                if (index >= 0) {
                    return XFor.substring(0, index);
                } else {
                    return XFor;
                }
            }
            ip = Xip;
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }

}